#include "datamanager.h"
#include "mysqlconnection.h"
#include <ctime>
#include <sstream>
#include <iostream>
#include <string>
#include "memexception.h"
#include <iomanip>

Member::Member()
{
    if(!con)
        con=connection.getConnection();
}

int Member::addMember(string memName, string memAddr, string memOccu)//, int* pMid)
{
    if (con == NULL)
        throw(DBException("database connection error"));
    int id = select(memName);

    if(id<1)
    {
        insert(memName,memAddr,memOccu);
        id=select(memName);
        return id;
    }
    else
        std::cout<<"member alreadly exists"<<std::endl;
        return 0;
}
void Member::insert(string memName, string memAddr, string memOccu) throw(DBException)
{

    pstmt = con->prepareStatement("insert into MEMBER (USER_NAME,ADDRESS,OCCUPATION) values(?,?,?);");
    //pstmt->setString(1,"admin");
    //pstmt->setString(2,log.logname);
    pstmt->setString(1,memName);
    pstmt->setString(2,memAddr);
    pstmt->setString(3,memOccu);

    bool is_success = pstmt->executeUpdate();
    if(is_success)
        cout << "insert success!" << endl;
    else
        cout << "insert fail!" << endl;
}

int Member::select(string name) throw(DBException)
{
    sql::ResultSet *result;
    pstmt = con->prepareStatement("select UID from MEMBER where USER_NAME=?;");
    pstmt -> setString(1,name);

    try{
    result=pstmt->executeQuery();

    int uid=-1;
    if(result->next())
    {
        uid=result->getInt("UID");
        return uid;
    }
    return uid;
    }
    catch(DBException)
    {
        return -1;
    }
}
Member::~Member()
{
    connection.~mysqlconnection();
}


Owns::Owns()
{
    if(!con)
        con=connection.getConnection();
}
Owns::~Owns()
{
    connection.~mysqlconnection();
}

void Owns::insert(string memName,string type,string startTime,
                  string endTime,int times,int statement)
    throw(DBException)
{
    pstmt = con->prepareStatement("insert into OWNS (USER_NAME,TYPE,START_TIME,END_TIME,TIMES,STATEMENT) values(?,?,?,?,?,?);");
    pstmt->setString(1,memName);
    pstmt->setString(2,type);
    pstmt->setString(3,startTime);
    pstmt->setString(4,endTime);
    pstmt->setInt(5,times);
    pstmt->setInt(6,statement);

    bool is_success = pstmt->executeUpdate();
    if(is_success)
        cout << "insert success!" << endl;
    else
        cout << "insert fail!" << endl;
}
int Owns:: addCard(string memberName,string cardType, int times)
{
    string startTime;
    string endTime;//有效时间

    if(cardType=="年卡")
    {
        time_t now = time (NULL);
        tm* local = localtime (&now);
        std::ostringstream starttime;
        starttime<<setfill('0')//空位填充
           <<setw(4)<<local->tm_year+1900
           <<setw(2)<<local->tm_mon+1
           <<setw(2)<<local->tm_mday;

        std::ostringstream endtime;
        endtime<<setfill('0')//空位填充
           <<setw(4)<<local->tm_year+1901
           <<setw(2)<<local->tm_mon+1
           <<setw(2)<<local->tm_mday;

        startTime=starttime.str();
        endTime=endtime.str();

        insert(memberName,cardType,startTime,endTime);
    }
    else if(cardType=="次卡")
    {
        time_t now = time (NULL);
        tm* local = localtime (&now);
        std::ostringstream starttime;
        starttime<<setfill('0')//空位填充
           <<setw(4)<<local->tm_year+1900
           <<setw(2)<<local->tm_mon+1
           <<setw(2)<<local->tm_mday;

        std::ostringstream endtime;
        endtime<<setfill('0')//空位填充
           <<setw(4)<<local->tm_year+1903
           <<setw(2)<<local->tm_mon+1
           <<setw(2)<<local->tm_mday;

        startTime=starttime.str();
        endTime=endtime.str();

        insert(memberName,cardType,startTime,endTime,times);
    } 
    return 0;
}


